package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import cn.hutool.http.HttpUtil;
@RestController
@RequestMapping("/tool/recognize")
public class RecController {
    @PostMapping("/ocr")
    public AjaxResult Ocr(@RequestParam("file") MultipartFile multipartFile)
    {
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        File file = null;
        try {
            file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String res= HttpUtil.createPost("http://localhost:5000/uploadOcr")
                .header("Content-Type","multipart/form-data")
                .form("file", file)
                .execute().body();
        System.out.println(res);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("result",res);
        return ajax;
    }

    @PostMapping("/asr")
    public AjaxResult Asr(@RequestParam("file") MultipartFile multipartFile)
    {
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        File file = null;
        try {
            file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String res= HttpUtil.createPost("http://localhost:5000/uploadAsr")
                .header("Content-Type","multipart/form-data")
                .form("file", file)
                .execute().body();
        System.out.println(res);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("result",res);
        return ajax;
    }
}
